import axios from 'axios'
import { Message } from 'element-ui'
const configUrl = {
  Base_api1_URL: process.env.NODE_ENV === 'development' ? '/pharmacyapitest' : process.env.VUE_APP_UPLOAD_API
}

const service = axios.create({
  timeout: 50000
})
service.interceptors.request.use(
  config => {
    console.log(configUrl.Base_api1_URL + config.url)
    config.url = configUrl.Base_api1_URL + config.url
    config.headers['CHANNEL-NO'] = 'WEB'
    return config
  },
  error => {
    Message({
      message: '发生错误',
      type: 'error',
      duration: 2000
    })
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code === 200) {
      return res
    } else {
      Message({
        message: res.msg || '发生错误',
        type: 'error',
        duration: 2000
      })
      return Promise.reject(new Error('发生错误'))
    }
  },
  error => {
    Message({
      message: '发生错误',
      type: 'error',
      duration: 2000
    })
    return Promise.reject(error)
  }
)

export default service
